<?php
/**
 * Created by PhpStorm.
 * User: minicart
 * Date: 2018/11/4
 * Time: 13:37.
 */

namespace app\models;

use think\Model;

class ReceivablesModel extends Model
{
    protected $table = 'edu_receivables';

    /**
     * 收款记录
     * modify by Small Y.
     *
     * @param $param
     *
     * @throws \think\exception\DbException
     *
     * @return array
     */
    public function getAccountRecharge($param)
    {
        $page = isset($param['page']) ? (int) $param['page'] : 1;
        $listRows = isset($param['limit']) ? (int) $param['limit'] : 10;
        $where = [];
        $where['x.id'] = $param['id'];
        if (4 === get_current_type() || 5 === get_current_type()) {
            $where['s.campus_id'] = get_current_campus_id();
        } elseif (2 === get_current_type() || 3 === get_current_type()) {
            $where['s.campus_id'] = ['in', get_current_campus_id()];
        }
        $listData = self::alias('s')
            ->join('edu_contract c', 'c.contract_number = s.contract_number')
            ->join('edu_student x', 'x.id = c.student_id')
            ->where($where)
            ->paginate($listRows, false, ['page' => $page])
            ->toArray()
        ;
        if (!empty($listData)) {
            foreach ($listData['data'] as &$item) {
                $item['payment_method'] = DictionaryModel::get($item['payment_method'])['name'];
                $item['amount'] = $item['amount'] / 100;
                if (1 === $item['status']) {
                    $item['status_text'] = '审核未通过';
                } elseif (2 === $item['status']) {
                    $item['status_text'] = '审核已通过';
                }
                $item['status'] = $item['amount'] / 100;
            }
        }

        return $listData;
    }

    /**
     * 订单列表 分页.
     *
     * @param array  $where
     * @param int    $page
     * @param int    $limit
     * @param string $order
     *
     * @throws \think\exception\DbException
     *
     * @return array
     */
    public static function pageLists($where = [], $page = 1, $limit = 5, $order = 'id desc')
    {
        $result = self::where($where)->order($order)->paginate($limit, false, ['page' => $page]);
        if (!$result->total()) {
            return ['code' => 0, 'msg' => 'ok', 'count' => 0, 'data' => []];
        }
        $amount = 0;
        foreach ($result as $key => $value) {
            $contract = ContractModel::get(['contract_number' => $value['contract_number']]);
            $gradeName = DictionaryModel::get($contract['grade'])['name'];
            $paymentMethodName = DictionaryModel::get($value['payment_method'])['name'];
            $campusName = CampusModel::get($value['campus_id'])['campus_name'];
            $result[$key]['grade'] = $gradeName ? $gradeName : '-';
            $result[$key]['campus_id'] = $campusName ? $campusName : '-';
            $result[$key]['payment_method'] = $paymentMethodName ? $paymentMethodName : '-';
            $result[$key]['student_name'] = $contract['student_name'] ? $contract['student_name'] : '-';
            $result[$key]['amount'] = sprintf('%.2f', ($value['amount'] / 100));
            $amount = $amount + $result[$key]['amount'];
            $name = UserModel::get($value['payee'])['name'];
            $result[$key]['payee'] = $name ? $name : '-';
        }

        $item_end = [];
        $item_end['contract_number'] = '合计';
        $item_end['student_name'] = '-';
        $item_end['grade'] = '-';
        $item_end['paydate'] = '-';
        $item_end['amount'] = sprintf('%.2f', $amount);
        $item_end['payment_method'] = '-';
        $item_end['receipt_number'] = '-';
        $item_end['flow_number'] = '-';
        $item_end['payee'] = '-';
        $item_end['campus_id'] = '-';
        $result[\count($result)] = $item_end;

        $result = $result->toArray();

        return ['code' => 0, 'msg' => 'ok', 'count' => $result['total'], 'data' => $result['data']];
    }
}
